<template>
  <div class="comment-container">
    <van-cell class="comment-item">
      <van-image class="avatar" fit="cover" slot="icon" round :src="comment.aut_photo"></van-image>
      <div class="title-wrap">
        <div class="uesr-name">{{comment.aut_name}}</div>
        <van-button class="like-btn" :class="{liked: comment.is_liking}" :icon="comment.is_liking ? 'good-job' : 'good-job-o'">{{comment.like_count || '赞'}}</van-button>
      </div>
      <div class="label">
        <p class="comment-content">{{comment.content}}</p>
        <div class="bottom-info">
          <span class="comment-pubdate">{{comment.pubdate | relativeTime}}</span>
          <van-button class="reply-btn" round @click="$emit('reply-click', comment)">回复 {{comment.reply_count}}</van-button>
        </div>
      </div>
    </van-cell>
  </div>
</template>

<script>
import { addCommentLike, deleteCommentLike } from '@/api/comment'
export default {
  name: 'CommentItem',
  props: {
    comment: {
      required: true,
      type: Object
    }
  },
  data () {
    return {
      commentLoading: false
    }
  },
  created () {
  },
  methods: {
    async onCommentLike () {
      this.commentLoading = true
      try {
        if (this.comment.is_liking) {
          // 已点赞，取消点赞
          await deleteCommentLike(this.comment.com_id)
          // 取消点赞，数量减减操作
          this.comment.like_count--
        } else {
          // 没有点赞，添加点赞
          await addCommentLike(this.comment.com_id)
          // 添加点赞，数量增加操作
          this.comment.like_count++
        }
        // 取反操作
        this.comment.is_liking = !this.comment.is_liking
      } catch (err) {
        // console.log(err)
        this.$toast('操作失败，请重试')
      }
      this.commentLoading = false
    }
  }
}
</script>

<style scoped lang="less">
.comment-item{
  .avatar{
    width: 60px;
    height: 60px;
    margin-right: 15px;
  }
  .title-wrap{
    display: flex;
    justify-content: space-between;
    align-items: center;
    .uesr-name{
      font-size: 16px;
      color: #406599;
    }
  }
  .comment-content{
    font-size: 14px;
    color: #222;
    word-break: break-all;
    text-align: justify;
  }
  .comment-pubdate{
    font-size: 12px;
    color: #222;
    margin-right: 25px;
  }
  .bottom-info{
    display: flex;
    align-items: center;
  }
  .reply-btn{
    width: 70px;
    height: 28px;
    line-height: 28px;
    font-size: 12px;
    color: #222;
  }
  .like-btn{
    width: auto;
    height: 30px;
    border: none;
    padding: 0;
  }
  .liked{
    color: #ee0a24;
  }
}
</style>
